from rest_framework.exceptions import ValidationError
from rest_framework_jwt.views import ObtainJSONWebToken

from apps.account.serializers import UserJWTSerializer
from utils import response_ok, response_error


# Create your views here.
class UserLoginView(ObtainJSONWebToken):
    serializer_class = UserJWTSerializer

    def post(self, request, *args, **kwargs):
        try:
            ser = self.serializer_class(data=request.data)

            if ser.is_valid(raise_exception=True):
                return response_ok(ser.validated_data)
        except ValidationError as e:
            import traceback
            traceback.print_exc()
            return response_error(e.__dict__)
